import 'package:flutter/material.dart';
import 'package:mqtt_client/mqtt_client.dart';
import 'package:ncov_2019/commom/data/const.dart';
import 'package:ncov_2019/mqtt/Mqtt.dart';

import '../../beans/Entity.dart';

/**
 * 机器安装设置
 */
class MachineSettings extends StatefulWidget {
  final App_esp_ip model;

  const MachineSettings({Key key, this.model}) : super(key: key);

  @override
  _MachineSettingsState createState() => _MachineSettingsState();
}

class _MachineSettingsState extends State<MachineSettings> {
  List<double> _showsliderValue = [0.0,0.0,0.0,0.0];
   List<double> _sliderValues1 = [0.0,0.0,0.0,0.0];
   List<double> _sliderValues2 = [0.0,0.0,0.0,0.0];
   List<double> _sliderValues3 = [0.0,0.0,0.0,0.0];
   List<double> _sliderValues4 = [0.0,0.0,0.0,0.0];

   List<String> singleTitle1 = ['快速度开门1时间','慢速度开门1时间','快速度关门1时间','快速度关门1时间']; //对应一个选项item = title+value
   List<String> singleTitle2 = ['xxx1','xxx1','xxx1','xxx1'];
   List<String> singleTitle3 =['xxx2','xxx2','xxx2','xxx2'];
   List<String> singleTitle4 = ['xxx3','xxx3','xxx3','xxx3'];

   List<double> _sliderValues5 = [0.0,0.0,0.0,0.0];
   List<double> _sliderValues6 =[0.0,0.0,0.0,0.0];
  List<String> doubleTitle1 = ['yyy','yyy','yyy','yyy'];
  List<String> doubleTitle2 = ['yy1','yy2','yy3','yy4'];
  List<Item> singleArgs = [];
   List<Item> doubleArgs =[];

  Data currentDeviceAllDatas;

  @override
  void initState() {
    super.initState();


//    currentDeviceAllDatas= Data(singleArgs,doubleArgs);
    singleArgs = [Item(_sliderValues1,singleTitle1),Item(_sliderValues2,singleTitle2),Item(_sliderValues3,singleTitle3),Item(_sliderValues4,singleTitle4)];
    doubleArgs =  [Item(_sliderValues5,doubleTitle1),Item(_sliderValues6,doubleTitle2)];
  }

  @override
  void dispose() {
    super.dispose();
    print(currentDeviceAllDatas);
    singleArgs.clear();
    doubleArgs.clear();
  }

  @override
  Widget build(BuildContext context) {
    var screenWidth = MediaQuery.of(context).size.width;
    return Scaffold(
//      appBar:
//      AppBar(
//        title: Text('参数设置'),
//      ),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            Card(
              margin: EdgeInsets.symmetric(horizontal: 20,vertical: 20),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Container(
                    padding: EdgeInsets.only(left: 20,top: 10,bottom: 10),
                    child: Row(
                      children: <Widget>[
                        Icon(Icons.settings_applications,color: Colors.blueAccent,),
//                    Icon(Icons.settings_brightness),
                        Text('单门参数设置:',style: TextStyle(color: Colors.blueAccent,fontWeight: FontWeight.bold),)
                      ],
                    ),
                  ),
                  ListTile(
                    onTap:()=>getPopWindow(context,true,0),
                    title: Text('点我设置门1开关门时间'),
                    trailing: Icon(Icons.keyboard_arrow_right),
                  ),
                  ListTile(
                    onTap:()=>getPopWindow(context,true,1),
                    title: Text('点我设置力度和关门延时时间'),
                    trailing: Icon(Icons.keyboard_arrow_right),
                  ),
                  ListTile(
                    onTap:()=>getPopWindow(context,true,2),
                    title: Text('点我设置开关门速度'),
                    trailing: Icon(Icons.keyboard_arrow_right),
                  ),
                ],
              ),
            ),
            Card(
              margin: EdgeInsets.symmetric(horizontal: 20,vertical: 30),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Container(
                    padding: EdgeInsets.only(left: 20,top: 10,bottom: 10),
                    child: Row(
                      children: <Widget>[
                        Icon(Icons.settings_applications,color: Colors.blueAccent,),
//                    Icon(Icons.settings_brightness),
                        Text('双门参数设置:',style: TextStyle(color: Colors.blueAccent,fontWeight: FontWeight.bold),)
                      ],
                    ),
                  ),
                  ListTile(
                    onTap:()=>getPopWindow(context,false,0),
                    title: Text('点我设置门2开关门时间'),
                    trailing: Icon(Icons.keyboard_arrow_right),
                  ),
                  ListTile(
                    onTap:()=>getPopWindow(context,false,1),
                    title: Text('点我设置力度和关门延时时间'),
                    trailing: Icon(Icons.keyboard_arrow_right),
                  ),
                ],
              ),
            ),

            Container(
              alignment: Alignment.center,
              margin: EdgeInsets.symmetric(horizontal: 20,vertical: 20),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
                  FlatButton(
                    textColor: Colors.black,
                        onPressed: (){},
                        child: Row(
                          children: <Widget>[
                            Icon(Icons.settings,color: Colors.blueAccent,),
                            Text('查看设置',style: TextStyle(fontSize: 15,fontWeight: FontWeight.bold),)
                          ],
                        ),
                  ),
                  FlatButton(
                    textColor: Colors.black,
                    onPressed: (){},
                    child: Row(
                      children: <Widget>[
                        Icon(Icons.assignment_late,color: Colors.orange,),
                        Text('行程学习',style: TextStyle(fontSize: 15,fontWeight: FontWeight.bold),)
                      ],
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: 20,
            ),
            Container(

              width: screenWidth/3,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Colors.blueAccent,
                borderRadius: BorderRadius.circular(10),
              ),
              padding: EdgeInsets.symmetric(vertical: 10),
              child: Text('提交设置',style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold),),
            ),
            SizedBox(
              height: 20,
            ),
            Container(
              alignment: Alignment.center,
              child: OutlineButton(
                onPressed: (){


                },
                color: Colors.blueAccent,
                textColor: Colors.blueAccent,
                disabledTextColor: Colors.blueAccent,
                child: Container(
                  width: 110,
                  child: Row(
                    children: <Widget>[
                      Icon(Icons.person,color: Colors.orange,),
                      Text('修改AP信息'),
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      )
    );
  }


//弹窗
  Future<void> getPopWindow(context,isSingleArg,itemIndex) {
    var tempArr = isSingleArg ? singleArgs: doubleArgs;//临时数组 List<Item>
    var tempItem = tempArr[itemIndex]; //点击的哪一个item
    var tempTitleArr = tempItem.itemTitle; //当前点击的item 所对应的标题。
    List<double> slideTempValues = [0,0,0,0];//当前点击的item 所对应的值
    return showDialog<void>(
      context: context,
      barrierDismissible: false, // user must tap button!
      builder: (BuildContext context) {
        return StatefulBuilder(
          builder: (context,state){
            return AlertDialog(
              title: Wrap(
                children: <Widget>[
                  Icon(Icons.security,color: Colors.blueAccent,),
                  Text('点我设置门1开关门时间',style: TextStyle(color: Colors.blueAccent,fontWeight: FontWeight.bold),),
                ],
              ),
              content: Container(
                padding: EdgeInsets.symmetric(horizontal: 10),
                height: MediaQuery.of(context).size.height/3,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(5),
                ),
                child: SingleChildScrollView(
                  child: Column(
                    children: List.generate(tempTitleArr.length, (index){
                      return  Card(
                        margin: EdgeInsets.symmetric(vertical: 5),
                        child: Column(
                          children: <Widget>[
                            ListTile(
                              title: Text('${tempTitleArr[index]}'),
                              leading: Icon(Icons.timer),
                            ),
                            Slider(
                              value: _showsliderValue[index],
                              onChanged: (data){
//                                print('change:$data');
                                state(() {
                                  _showsliderValue[index] = data;
                                });
                              },
                              onChangeStart: (data){
//                                print('start:$data');
                              },
                              onChangeEnd: (data){
//                                print('end:$data');
//                                _sliderValue = data;
                                slideTempValues[index] = data;//拿到最后值了。
                                if(isSingleArg){
                                  singleArgs[itemIndex] = Item(slideTempValues,tempTitleArr);
//                                  print(slideTempValues);
//                                  print(singleArgs);
                                  currentDeviceAllDatas= Data(singleArgs,[]);
                                }else{
                                  doubleArgs[itemIndex]=Item(slideTempValues,tempTitleArr);
                                  currentDeviceAllDatas= Data([],doubleArgs);
                                }

                              },
                              min: 0,
                              max: 120,
                              divisions: 4,
                              label: '${_showsliderValue[index]}',
                              activeColor: Colors.blueAccent,
                              inactiveColor: Colors.grey,
                              semanticFormatterCallback: (double newValue) {
                                return '${newValue.round()} dollars}';
                              },
                            ),
                          ],
                        ),
                      );
                    }),
                  ),
                ),
              ),
              actions: <Widget>[
                FlatButton(
                  child: Text('取消'),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
                FlatButton(
                  child: Text('确定'),
                  onPressed: () {
                    //TODO 全部拿到的数据,点击确定按钮发送网络请求
                    print(currentDeviceAllDatas);
                    _showsliderValue = [0.0,0.0,0.0,0.0];//重置临时数组
                    Navigator.of(context).pop();
                  },
                ),
              ],
            );
          },
        );
      },
    );

  }
}



